VB.NET Technology

VB.Net Projects

VB.Net Project 1

Inheritance
Previous Home Next
adplus-dvertising

In VB.Net, the specialization relationship is generally implemented by using inheritance. Inheritance is also provides the reusability, or we can say that extracts some features from one class to another class.

Class Bird
	Public Sub New()
		Console.WriteLine("Bird constructor")
	End Sub
	Public Sub Greet()
		Console.WriteLine("Bird says Hello")
	End Sub
	Public Sub Talk()
		Console.WriteLine("Bird talk")
	End Sub
	Public Overridable Sub Sing()
		Console.WriteLine("Bird song")
	End Sub
End Class
Class Peacock
	Inherits Bird
	Public Sub New()
		Console.WriteLine("Peacock constructor")
	End Sub
	Public Shadows Sub Talk()
		Console.WriteLine("Peacock talk")
	End Sub
	Public Overrides Sub Sing()
		Console.WriteLine("Peacock song")
	End Sub
End Class
Dim a1 As New Bird()
a1.Talk()
a1.Sing()
a1.Greet()
Dim a2 As Bird = New Peacock()
a2.Talk()
a2.Sing()
a2.Greet()

Types of Inheritance

In Object Oriented Programming concept there are 3 types of inheritances.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance

Single Inheritance :

Public Class A
End Class
Public Class B
	Inherits A
End Class

Multiple Inheritance :

Public Class A
End Class
Public Class B
End Class
Public Class C
	Inherits A
	Inherits B
End Class

Multilevel Inheritance

Public Class A
End Class
Public Class B
	Inherits A
End Class
Public Class C
	Inherits B
End Class

In the above three types VB.net don't proved Multiple Inheritance. As there is conflict of multiple override methods in base classes (say A, B in above example) As in Place VB.net give another feature called Interfaces using interfaces you can achieve multiple Inheritance feature.

Polymorphism

Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details.

Creating Polymorphic Types

For creating polymorphism there are two steps

  1. Create a base class with virtual methods.
  2. Create derived classes that override the behavior of the base class’s virtual methods.

To create a method in a base class that supports polymorphism, mark the method as virtual.

Example

Public Class BaseClass
	Public Overridable Sub DoWork()
	End Sub
	Public Overridable ReadOnly Property WorkProperty() As Integer
		Get
			Return 0
		End Get
	End Property
End Class
Public Class DerivedClass
	Inherits BaseClass
	Public Overrides Sub DoWork()

	End Sub
	Public Overrides ReadOnly Property WorkProperty() As Integer
		Get
			Return 0
		End Get
	End Property
End Class
Previous Home Next